package com.warehouse.controller;

import com.warehouse.common.Result;
import com.warehouse.entity.StockOut;
import com.warehouse.service.StockOutService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/api/stock-out")
@CrossOrigin(origins = "*")
public class StockOutController {

    @Autowired
    private StockOutService stockOutService;

    @GetMapping
    public Result<List<StockOut>> getAllStockOuts() {
        List<StockOut> stockOuts = stockOutService.getAllStockOuts();
        return Result.success(stockOuts);
    }

    @GetMapping("/{id}")
    public Result<StockOut> getStockOutById(@PathVariable Long id) {
        StockOut stockOut = stockOutService.getStockOutById(id);
        if (stockOut != null) {
            return Result.success(stockOut);
        }
        return Result.error("出库记录不存在");
    }

    @GetMapping("/order/{orderId}")
    public Result<List<StockOut>> getStockOutsByOrderId(@PathVariable Long orderId) {
        List<StockOut> stockOuts = stockOutService.getStockOutsByOrderId(orderId);
        return Result.success(stockOuts);
    }

    @GetMapping("/product/{productId}")
    public Result<List<StockOut>> getStockOutsByProductId(@PathVariable Long productId) {
        List<StockOut> stockOuts = stockOutService.getStockOutsByProductId(productId);
        return Result.success(stockOuts);
    }

    @GetMapping("/date-range")
    public Result<List<StockOut>> getStockOutsByDateRange(
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startTime,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endTime) {
        List<StockOut> stockOuts = stockOutService.getStockOutsByDateRange(startTime, endTime);
        return Result.success(stockOuts);
    }

    @GetMapping("/customer")
    public Result<List<StockOut>> getStockOutsByCustomer(@RequestParam String customer) {
        List<StockOut> stockOuts = stockOutService.getStockOutsByCustomer(customer);
        return Result.success(stockOuts);
    }

    @GetMapping("/status/{status}")
    public Result<List<StockOut>> getStockOutsByStatus(@PathVariable String status) {
        List<StockOut> stockOuts = stockOutService.getStockOutsByStatus(status);
        return Result.success(stockOuts);
    }

    @PostMapping
    public Result<StockOut> createStockOut(@RequestBody StockOut stockOut) {
        StockOut createdStockOut = stockOutService.createStockOut(stockOut);
        return Result.success(createdStockOut);
    }

    @PostMapping("/from-order")
    public Result<Boolean> createStockOutFromOrder(@RequestParam Long orderId, @RequestParam String operator) {
        boolean success = stockOutService.createStockOutFromOrder(orderId, operator);
        if (success) {
            return Result.success(true);
        }
        return Result.error("生成出库单失败，请检查库存是否充足");
    }

    @PostMapping("/process/{id}")
    public Result<Boolean> processStockOut(@PathVariable Long id, @RequestParam String operator) {
        boolean success = stockOutService.processStockOut(id, operator);
        if (success) {
            return Result.success(true);
        }
        return Result.error("处理出库失败，请检查库存是否充足");
    }

    @PutMapping("/{id}")
    public Result<Boolean> updateStockOut(@PathVariable Long id, @RequestBody StockOut stockOut) {
        stockOut.setId(id);
        boolean success = stockOutService.updateStockOut(stockOut);
        if (success) {
            return Result.success(true);
        }
        return Result.error("更新出库记录失败");
    }

    @DeleteMapping("/{id}")
    public Result<Boolean> deleteStockOut(@PathVariable Long id) {
        boolean success = stockOutService.deleteStockOut(id);
        if (success) {
            return Result.success(true);
        }
        return Result.error("删除出库记录失败");
    }
} 